﻿/*
AwpAdmin, an admin tool for Battlefield 3
Copyright (C) 2011 agentwite, Timi, Unseen, AlCapwn

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

You can contact us at http://bf3admin.codeplex.com/.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Battlefield3;
using System.Globalization;

namespace AwpAdminTool
{
    /// <summary>
    /// Contains extension methods for AwpAdmin.
    /// </summary>
    public static class Extensions
    {
        static CultureInfo c = CultureInfo.GetCultureInfo("en-US");

        internal static bool HasValue(this string str)
        {
            return !string.IsNullOrEmpty(str);
        }

        internal static string ToHexString(this byte[] bytes)
        {
            StringBuilder sb = new StringBuilder(bytes.Length * 2);
            for (int i = 0; i < bytes.Length; i++)
            {
                //fxcop says to supply an iformatprovider
                sb.Append(bytes[i].ToString("x2", c));
            }
            return sb.ToString();
        }

        /// <summary>
        /// Gets an <see cref="Admin"/> from the specified <paramref name="player"/>, for the specified <paramref name="awpAdmin"/>.
        /// </summary>
        /// <param name="player">The target <see cref="Player"/>.</param>
        /// <param name="awpAdmin">The target <see cref="AwpAdmin"/>.</param>
        /// <returns>An <see cref="Admin"/> from the specified <paramref name="player"/>.</returns>
        /// <remarks>
        /// <see cref="GetAdmin(Player)"/> should be used, if possible, because it doesn't require the <see cref="AwpAdmin"/>
        /// to be supplied.
        /// </remarks>
        public static Admin GetAdmin(this Player player, AwpAdmin awpAdmin)
        {
            return new Admin(awpAdmin, player);
        }

        /// <summary>
        /// Gets an <see cref="Admin"/> from the specified <paramref name="player"/>.
        /// </summary>
        /// <param name="player">The target <see cref="Player"/>.</param>
        /// <returns>An <see cref="Admin"/> from the specified <paramref name="player"/>.</returns>
        public static Admin GetAdmin(this Player player)
        {
            return new Admin(AwpAdmin.GetAwpAdmin(player.Parent), player);
        }

        /// <summary>
        /// Gets whether or not the <see cref="Player"/> is an <see cref="Admin"/>.
        /// </summary>
        /// <param name="player">The target <see cref="Player"/>.</param>
        /// <returns>True if the <see cref="Player"/> is an <see cref="Admin"/>.</returns>
        public static bool IsAdmin(this Player player)
        {
            foreach (var e in AwpAdmin.GetAwpAdmin(player.Parent).AdminSettingsCopy.Elements(AwpAdmin.XmlNamespace + "admin"))
                //only check for 0 cos min is 0
                if (e.Attribute("guid").Value == player.Guid && e.Attribute("rank").Value != "0") return true;
            return false;
        }
    }
}
